<?php 
session_start();

$post=0;
$yearerr="";
$year="";
$term="";
$course="";
$courseerr="";

include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=2)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
if(!$con)
{
	die("Error: ".mysql_error());
	}
mysql_select_db($_DB_NAME,$con);

//validation needs to be handled

if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$term=$_POST['semesterType'];
		$year=$_POST['semYear'];
		$course=$_POST["courseid"];
	
	if (empty($year))
    	{
			$yearerr = "Year is required";
		}
		else
		{
			$year = clean_input($year);
			if (!preg_match("/^[0-9]{4}$/",$year))
			   {
					  $yearerr = "Invalid Year format";
			   }
		}
		
		if(empty($course))
		{
			$courseerr = "Course is required";
			$course = clean_input($course);
		}
  		if($yearerr=="" and $courseerr=="")
			 {
				 
					mysql_query("set @semtypte='".$term."'");
					mysql_query("set @openyear=".$year);
				
					$semidrs = mysql_query("select semid from semester where term=@semtypte and year=@openyear");
					if($semidrs == false )
					{
						die(mysql_error() );}
					$semid = mysql_fetch_assoc($semidrs);

					mysql_query("set @semid=".$semid["semid"]);
					mysql_query("set @courseid=".$course);
					mysql_query("set @username='".$_SESSION["Username"]."'");
					$result = mysql_query("Select u.firstname,u.lastname, s.Emailid,d.description, s.program,s.year, s.GPA,s.TEPAIC,s.Experience from users u, students s, degree d where u.username=s.username and d.degreeid=s.degree and u.username in (Select h.username from hire h where h.openingid in(select o.openingid from openings o, semester s where s.semid=o.semid and o.instructor='".$_SESSION["Username"]."' and s.term='".$term."' and s.year='".$year."' and o.courseid='".$course."' and o.isdeleted=0))");	 
					if($result == false)
						{
							die(mysql_error());
						}
						$post=1;
				}
		}

function clean_input($data)
	{
	  $data = trim($data);
	  $data = stripslashes($data);
	  $data = htmlspecialchars($data);
	  return $data;
	}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hiring</title>
<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    $('#datatables').dataTable(
	{	"bJQueryUI":true
		});
});
</script>

<script>
function set(openingid)
{	

	 //var hashid=CryptoJS.SHA256(openingid);
	  window.location.href = 'apply.php?openingid='+openingid 

 	}
</script>	

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  
	 <script type="text/javascript">
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		function courseSuggest()
		{
    				$.ajax({
						type: "POST",
                		url: "professorcourses.php",
                		data: {term: $('#term').val(), year :$('#year').val() , courseid: $("#courseid").val()},
					    success: function(courses){
									$("#courseid").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
			
		}
		</script>
<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />

</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img alt="Indiana University" height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey" >
</div>
	<center>
			<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->
              <ul>
              	<li style="width:200px;" ><a href="comment.php">Comment</a></li>
                <li style="width:200px;"><a href="profhire.php">Hiring</a></li>
              </ul>
      	</div>
        </center>
        <span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
        <div class="footbody">
       
        <form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" >
          <table class="SemesterTable">
          <thead>
          <tr>
          <td>
           <label class="Header1"> AI Openings</label>
          </td>
          </tr>
          </thead>
        <tbody>
        	<tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
         	  <tr>
                  <td>
                  	<label style="color:#990000" >Select a semester </label>
                  </td>
                  <td>
                      <select name="semesterType" id="term" class="crimson-text" >
                    
                     <option value="Fall"<?php
					  if (isset($semType) && $semType=="Fall") echo "selected='selected'";
					  else echo "";?>>Fall</option>
                   <option value="Spring"<?php
					  if (isset($semType) && $semType=="Spring") echo "selected='selected'";
					  else echo "";?>>Spring</option>

                      </select>
                  </td>
                  </tr>
                  <tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
                  <tr>
                  <td>
                    <label class="crimson-text"> Year</label>
                  </td>
                  <td>
                    <input type="text"  name="semYear" id="year" class="crimson-border" value="<?php echo $year;?>" onkeyup="populateYear()" />
                  </td>
                  <td class="error" >
     <?php echo $yearerr;?>
        </td>
                  </tr>
                  <tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
             <tr>
                   <td>
                  <label style="color:#990000" class="left-alignmentlev1">Select a course </label>
                  </td>
                  <td>
                   <input id="courseid" name="courseid" type="text" class="crimson-border"  value="<?php echo $course;?>" onkeyup="courseSuggest()"/>
                  </td>
                   <td class="error">
     <?php echo $courseerr;?>
                  </td>
                </tr>
                  <tr>
                  <td>
                    <input type="submit" value="View" />
                  </td>
                  </tr>
                  </tbody>
                  </table>
                  </form>
        <div class="datatable-container">
    <table id="datatables" class="display">
          <thead>
          <tr>
          <th>Name</th>
          <th>Email ID</th>
          <th>Degree</th>
          <th>Program</th>
          <th>Year</th>
          <th>GPA</th>
          <th>TEPAIC</th>
          <th>Experience</th>
          </tr>
          </thead>
          <tbody>
          <?php 
		  if($post==1)
		  {
		  while($row=mysql_fetch_array($result)){
			  ?>
              <tr>
              <td ><?=$row['firstname'].$row['lastname']?></td>
              <td><?=$row['Emailid']?></td>
              <td><?=$row['description']?></td>
              <td><?=$row['program']?></td>
              <td><?=$row['year']?></td>
              <td><?=$row['GPA']?></td>
              <td><?=$row['TEPAIC']?></td>
              <td><?=$row['Experience']?></td>
              </tr>
              <?php } }?>
          </tbody>
		</table>
       </div>
       </div>
</body>
</html>